package com.ip.getip;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
 * @ Description   :  浏览器爬虫
 * @ Author        :  leixin
 * @ CreateDate    :  2019/12/24-下午3:05
 * @ Version       :  1.0
 */

public class MySelenium {

    public static void main(String[] args) throws Exception {
//        System.setProperty("webdriver.gecko.driver", "D:\\driver\\geckodriver.exe");
        FirefoxOptions firefoxOptions = new FirefoxOptions();
        FirefoxProfile firefoxProfile = new FirefoxProfile();
        firefoxProfile.setPreference("permissions.default.image", 2);
        firefoxOptions.addArguments("--headless");
        firefoxOptions.setProfile(firefoxProfile);

        WebDriver driver = new FirefoxDriver(firefoxOptions);

        driver.get("https://cherry31.com/#/film");
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

        //点击登录
        WebElement driverElement = driver.findElement(By.partialLinkText("登录"));

        driverElement.sendKeys(Keys.ENTER);
        //输入账号密码
        WebElement accountNumber = driver.findElement(By.xpath("/html/body/div/div/div[2]/div[1]/div[2]/div[1]/input"));
        accountNumber.sendKeys("18108192859");
        WebElement passwd = driver.findElement(By.xpath("/html/body/div/div/div[2]/div[1]/div[2]/div[2]/input"));
        passwd.sendKeys("991426");
        WebElement confirm = driver.findElement(By.xpath("/html/body/div/div/div[2]/div[1]/div[2]/div[4]"));
        //登录成功
        confirm.click();
//
//        (new WebDriverWait(driver, 100)).until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div/span/div[1]/div/div[1]/div[4]/div/button[4]/span")));
//
//        //翻页
//        WebElement element = driver.findElement(By.xpath("/html/body/div/span/div[1]/div/div[1]/div[4]/div/button[4]/span"));
//        element.click();
        //获取当前主页
        String index = driver.getWindowHandle();
        //等待元素出现
        (new WebDriverWait(driver, 30)).until(ExpectedConditions
            .presenceOfElementLocated(By.className("film-item")));
        for (int i = 1; i < 25; i++) {
            System.out.println(info(driver, index, i));
        }

//        List<WebElement> elements = driver.findElements(By.className("video_title"));
//        for (WebElement w:elements){
//            System.out.println(w.getText());
//        }
//        driver.getPageSource();
//        driver.quit();
    }

    public static String info(WebDriver driver, String index, int i) throws InterruptedException {
        WebElement element = driver
            .findElement(By.xpath("/html/body/div/span/div[1]/div/div[1]/div[3]/div[" + i + "]/div[2]"));
        String js = "arguments[0].click()";
        ((JavascriptExecutor) driver).executeScript(js, element);
        //获取详情页面
        String info = driver.getWindowHandle();
        //切换到详情页面
        for (String winHandle : driver.getWindowHandles()) {
            if (winHandle.equals(index)) {
                continue;
            }
            if (winHandle.equals(info)) {
                driver.switchTo().window(winHandle);
            }
            break;
        }
        System.out.println(element.getText());
        (new WebDriverWait(driver, 100)).until(ExpectedConditions
            .presenceOfElementLocated(By.xpath("/html/body/div/span/div[1]/div/div[2]/div[3]/div[1]/div[2]")));
        WebElement element1 = driver.findElement(By.id("add-bnt"));
        ((JavascriptExecutor) driver).executeScript(js, element1);
        (new WebDriverWait(driver, 100)).until(ExpectedConditions
            .presenceOfElementLocated(By.id("downloadSvg")));
        WebElement element2 = driver.findElement(By.id("downloadSvg"));
        ((JavascriptExecutor) driver).executeScript(js, element2);
        Thread.sleep(2000);
        //进入到下载页
        for (String winHandle : driver.getWindowHandles()) {
            if (winHandle.equals(index)) {
                continue;
            }
            if (winHandle.equals(info)) {
                continue;
            }
            driver.switchTo().window(winHandle);
            break;
        }
        System.out.println(driver.getCurrentUrl());
        return driver.getCurrentUrl();
    }
}
